package com.totoro.web;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;


import com.totoro.domain.MySysRemarkTemplate;
import com.totoro.domain.Project;
import com.totoro.domain.PublicReply;
import com.totoro.domain.ReviewProject;
import com.totoro.domain.SysRemarkTemplate;
import com.totoro.domain.Tutor;
import com.totoro.domain.Tutorial;
import com.totoro.service.ProjectService;
import com.totoro.service.ReviewProjectService;
import com.totoro.service.StaffService;
import com.totoro.service.SysRemarkTemplateService;
import com.totoro.service.TutorialService;

@Controller
@RequestMapping("/tutor")
public class TutorialController {

	@Autowired
	private TutorialService tutorialService;

	@Autowired
	private StaffService staffService;

	@Autowired
	private ProjectService projectService;
	
	@Autowired ReviewProjectService reviewProjectService;
	// 系统评语模板
	@Autowired
	private SysRemarkTemplateService sysRemarkTemplateService;

	// 指导教师评审列表
	@RequestMapping("/tutorialList")
	public ModelAndView list(Authentication authentication) {
		// 获取用户名
		String username = authentication.getName();
		System.out.println("用户名：" + username);
		Tutor tutor = staffService.uniqueResult("no", username);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/tutor/tutorialList");
		mav.addObject("tutorialList", tutor.getTutorial());
		return mav;
	}

	// 指导教师评审页面
	@RequestMapping(value = "/tutorReview", method = RequestMethod.GET)
	public ModelAndView toTutorReview(Integer project_id, Integer tutor_id,
			ModelMap modelMap) {
		Project project = new Project();
		project.setId(project_id);
		Tutor tutor = new Tutor();
		tutor.setId(tutor_id);

		Tutorial tutorial = tutorialService.uniqueTutorial(tutor, project);
		modelMap.addAttribute("tutorial", tutorial);
		
		ModelAndView mv = new ModelAndView();
		mv.addObject("sys", sysRemarkTemplateService.list());
		mv.setViewName("/tutor/tutorReview");
		return mv;
	}

	// 保存指导教师评审
	@RequestMapping(value = "/tutorReview", method = RequestMethod.POST)
	public String saveTutorReview(@ModelAttribute("tutorial") Tutorial tutorial) {
		tutorial.setState(true);
		tutorialService.update(tutorial);
		return "redirect:/tutor/tutorialList.html";
	}

	// 暂存指导教师评审
	@RequestMapping(value = "/tutorReviewTag", method = RequestMethod.POST)
	public String saveTutorReviewTag(
			@ModelAttribute("tutorial") Tutorial tutorial) {
		tutorial.setState(false);
		tutorialService.saveOrUpdate(tutorial);
		return "redirect:/tutor/tutorialList.html";
	}

	// 打印评审表excel格式
	@RequestMapping(value = "/showTutorialByXls")
	public String toExceAndView(ModelMap model, Integer project_id,
			Integer tutor_id) {
		Project project = new Project();
		project.setId(project_id);
		Tutor tutor = new Tutor();
		tutor.setId(tutor_id);
		// 加载指导教师评审
		Tutorial tutorial = tutorialService.uniqueTutorial(tutor, project);
		model.addAttribute("tutorial", tutorial);
		return "tutorialExcel";
	}

	// 打印总成绩表excel格式
	@RequestMapping(value = "/getTotalScoreByXls")
	public String totoaScoreExcelAndView(ModelMap model, Integer projectId) {
		//得到当前课题
		Project project = projectService.get(projectId);
		
		//根据课题得到指导平均成绩
		Tutorial averageTutorial = tutorialService.getAverageTutorialScoreByProject(project);
		//得到答辩成绩
		//由于没有标记位标记是否已录入，依次判断
		PublicReply publicReply = project.getPublicReply();
		if (publicReply != null) {
			if (publicReply.getAchievementLevel() == null) {
				publicReply.setAchievementLevel("0");
			}
			if (publicReply.getCompleteness() == null) {
				publicReply.setCompleteness("0");
			}
			if (publicReply.getReply() == null) {
				publicReply.setReply(0);
			}
			if (publicReply.getStatement() == null) {
				publicReply.setStatement(0);
			}
		} else {
			publicReply = new PublicReply();
			publicReply.setAchievementLevel("0");
			publicReply.setCompleteness("0");
			publicReply.setReply(0);
			publicReply.setStatement(0);
		}
		
		//根据课题得到评阅平均成绩
		ReviewProject averageReviewProject = reviewProjectService.getAverageReviewScoreByProject(project);
		
		
		model.addAttribute("publicReply", publicReply);
		model.addAttribute("reviewProject", averageReviewProject);
		model.addAttribute("tutorial", averageTutorial);
		model.addAttribute("project", project);
	
		return "totalScoreExcel";
	}
	// 答辩小组意见
	@RequestMapping("/dabianxiaozuyijianbiao")
	public String toPubilcReplyAdvice(Integer projectId, Integer tutorId) {
	    return "redirect:/publicReply/publicReplyAdvice.html?projectId=" + projectId + "&tutorId=" + tutorId;
	}
	
	 // 保存评语模板
    @RequestMapping(value = "/saveAsTemplate", method = RequestMethod.POST)
    public Object saveAsTemplate(SysRemarkTemplate sysRemarkTemplate, Authentication authentication, Model model) {
//        Tutor tutor = staffService.uniqueResult("no", authentication.getName());
//        sysRemarkTemplate.setActor(tutor);
        sysRemarkTemplateService.saveOrUpdate(sysRemarkTemplate);
        
        return "sysRemarkTemplateJson";
    }
    
    @RequestMapping(value = "/getRemarkTemplateKeywords", method = RequestMethod.POST)
    public Object getRemarkTemplateKeywords(Authentication authentication, Model model, String keyword) {
//        Tutor tutor = staffService.uniqueResult("no", authentication.getName());
        List<SysRemarkTemplate> list = sysRemarkTemplateService.list();
        
        boolean isDuplicated = false;
        
        for (SysRemarkTemplate template : list) {
            if (template.getKeyword().equals(keyword)) {
                isDuplicated = true;
                break;
            }
        }
        model.addAttribute("isDuplicated", isDuplicated);
        
        return "remarkTemplateKeywordsJson";
    }
    
}
